package com.xyhuo.leetcode.y2025;

import java.util.LinkedList;
import java.util.List;

public class MyStack {

    private List<Integer>[] list;

    private int currentIndex = 0;

    public MyStack() {
        list = new List[2];
        list[0] = new LinkedList<>();
        list[1] = new LinkedList<>();
    }

    public void push(int x) {
        list[currentIndex].add(x);
    }

    public int pop() {
        if (list[currentIndex].size() == 0) {
            return 0;
        }
        int anotherIndex = currentIndex == 0 ? 1 : 0;
        int index = 0;
        int ans = 0;
        while (true) {
            if (list[currentIndex].size() == 1) {
                ans = list[currentIndex].get(0);
                break;
            }
            Integer num = list[currentIndex].get(index);
            list[anotherIndex].add(num);
            list[currentIndex].remove(0);
        }
        list[currentIndex].clear();
        currentIndex = anotherIndex;
        return ans;
    }

    public int top() {
        return list[currentIndex].get(list[currentIndex].size() - 1);
    }

    public boolean empty() {
        return list[currentIndex].isEmpty();
    }

    public static void main(String[] args) {
        MyStack m = new MyStack();
        m.push(1);
        int pop = m.pop();
        boolean empty = m.empty();
    }
}
